using UnityEngine;

[ExecuteInEditMode]
public class ControllerRollingBallSpawner : ControllerGeneric
{
	private Vector3 fixedScale = new Vector3(1f, 1f, 1f);

	public GameObject rollingBall;

	public float delay;

	public float interval = 1f;

	public float intervalVariation;

	public float fallDistance = 100f;

	public GameObject ballDummy;

	public override void OnLevelInPlace(Transform levelTransform)
	{
		base.OnLevelInPlace(levelTransform);
		if (levelTransform == base.transform.root)
		{
			PrepareForGame();
		}
	}

	public void PrepareForGame()
	{
		RollingBallSpawner rollingBallSpawner = base.gameObject.AddComponent<RollingBallSpawner>();
		rollingBallSpawner.rollingBall = rollingBall;
		rollingBallSpawner.delay = delay;
		rollingBallSpawner.interval = interval;
		rollingBallSpawner.intervalVariation = intervalVariation;
		rollingBallSpawner.fallDistance = fallDistance;
		remoteControl.controller = null;
		if (Application.isPlaying)
		{
			Object.Destroy(ballDummy);
			Object.Destroy(this);
		}
		else
		{
			Object.DestroyImmediate(ballDummy);
			Object.DestroyImmediate(this);
		}
	}

	private void Update()
	{
		if (Application.isEditor && !Application.isPlaying)
		{
			SetupRemoteControl();
			if (IsRemoteControlActive())
			{
				base.transform.position = remoteControl.transform.position;
				base.transform.rotation = remoteControl.transform.rotation;
			}
			if (remoteControl != null)
			{
				remoteControl.transform.localScale = fixedScale;
				remoteControl.scale = fixedScale;
			}
			Setup();
		}
	}

	private void Setup()
	{
		AssureParenting();
	}

	public void SetupRemoteControl()
	{
		if (remoteControl == null)
		{
			InitRemoteControl();
			remoteControl.gameObject.name = remoteControl.gameObject.name + base.name;
			remoteControl.gameObject.transform.position = base.transform.position;
			remoteControl.gameObject.transform.rotation = base.transform.rotation;
			remoteControl.gameObject.transform.localScale = fixedScale;
			remoteControl.gameObject.AddComponent<PresetsRollingBallSpawner>();
		}
	}

	public override void CopyFromRemoteControl(RemoteControl rc)
	{
		base.CopyFromRemoteControl(rc);
		CopyParentingFromRemoteControl(rc);
		ControllerRollingBallSpawner component = rc.controller.GetComponent<ControllerRollingBallSpawner>();
		delay = component.delay;
		interval = component.interval;
		intervalVariation = component.intervalVariation;
		PresetsRollingBallSpawner component2 = remoteControl.gameObject.GetComponent<PresetsRollingBallSpawner>();
		component2.delay = component.delay;
		component2.interval = component.interval;
		component2.intervalVariation = component.intervalVariation;
	}
}
